//
//  WKViewController.m
//  WKDemo
//
//  Created by wangzhaohui-Mac on 14-8-13.
//  Copyright (c) 2014年 com.app. All rights reserved.
//
#import "WKBaseViewController.h"
#import "WKTitleView.h"
#import "WKComposeViewController.h"
#import "WKTitleButton.h"

#define WKTitleBtnW 75
#define WKTitleH 44
#define WKTitleW 220
#define WKChildViewH 372

typedef enum{
    ButtonTypeOne,
    ButtonTypeTwo,
    ButtonTypeThree
} ButtonType;

@interface WKBaseViewController () <UICollectionViewDataSource,UICollectionViewDelegate>

/** 当前视图页码*/
@property (nonatomic,assign) int currentPage;

@end

@implementation WKBaseViewController

#pragma mark - 初始化方法
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //设置导航栏标题视图
    self.navigationItem.titleView = [self setupTitleView];
    
    //设置导航栏左右侧按钮
    [self setupNavigationBarButtonItem];
    //切换视图
    [self chooseView];
}

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    
    self.scrollView.height = 679;
    self.scrollView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
}
/**
 *  设置标题按钮
 */
- (UIView *)setupTitleView
{
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(50, 0, WKTitleW, WKTitleH)];
    titleView.backgroundColor = [UIColor clearColor];
    
    //精华按钮
    WKTitleButton *newestBtn = [[WKTitleButton alloc] initWithFrame:CGRectMake(0, 0, WKTitleBtnW, WKTitleH)];
//    newestBtn.backgroundColor = [UIColor clearColor];
    newestBtn.tag = ButtonTypeOne;
    [newestBtn setImage:[UIImage imageNamed:@"nav-order-newest"] forState:UIControlStateNormal];
    [newestBtn setImage:[UIImage imageNamed:@"nav-order-newest-click"] forState:UIControlStateSelected];
    [newestBtn setTitle:@"精华" forState:UIControlStateNormal];

    [titleView addSubview:newestBtn];
    [newestBtn addTarget:self action:@selector(cilck:) forControlEvents:UIControlEventTouchDown];
    self.selectedBtnOne = newestBtn;
    
    
    //最新按钮
    WKTitleButton *newBtn = [[WKTitleButton alloc] initWithFrame:CGRectMake(75, 0, WKTitleBtnW, WKTitleH)];
//    newBtn.backgroundColor = [UIColor redColor];
    newBtn.tag = ButtonTypeTwo;
    [newBtn setImage:[UIImage imageNamed:@"nav-order-new"] forState:UIControlStateNormal];
    [newBtn setImage:[UIImage imageNamed:@"nav-order-new-click"] forState:UIControlStateSelected];
    
    [newBtn setTitle:@"最新" forState:UIControlStateNormal];
    
    [titleView addSubview:newBtn];
    [newBtn addTarget:self action:@selector(cilck:) forControlEvents:UIControlEventTouchDown];
    self.selectedBtnTwo = newBtn;
    
    
    //穿越按钮
    WKTitleButton *randomBtn = [[WKTitleButton alloc] initWithFrame:CGRectMake(150, 0, WKTitleBtnW, WKTitleH)];
//    randomBtn.backgroundColor = [UIColor greenColor];
    randomBtn.tag = ButtonTypeThree;
    [randomBtn setImage:[UIImage imageNamed:@"nav-order-random"] forState:UIControlStateNormal];
    [randomBtn setImage:[UIImage imageNamed:@"nav-order-random-click"] forState:UIControlStateSelected];

    [randomBtn setTitle:@"穿越" forState:UIControlStateNormal];
    
    [titleView addSubview:randomBtn];
    [randomBtn addTarget:self action:@selector(cilck:) forControlEvents:UIControlEventTouchDown];
    self.selectedBtnThree = randomBtn;
    
    return titleView;
    
}
/**设置导航栏左右侧按钮*/
- (void)setupNavigationBarButtonItem
{
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemImageName:@"navigationButtonPublish" highlightImage:@"navigationButtonPublishClick" target:self action:@selector(leftCompile)];
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemImageName:@"navigationButtonRefresh" highlightImage:@"navigationButtonRefreshClick" target:self action:@selector(rightRefresh:)];
}

/**
 *  切换视图
 */
- (void)chooseView
{
    
    //创建一个布局对象
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    // 设置每一个cell的宽高 (cell在CollectionView中称之为item)
    layout.itemSize = CGSizeMake(480,320);
    // 设置item行与行之间的间隙
    layout.minimumLineSpacing = 0;
    //设置滚动方向(水平方向)
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
 
	UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 320, 480) collectionViewLayout:layout];
    //设置分页
    collectionView.pagingEnabled = YES;
   
    //隐藏滑动条
    collectionView.showsVerticalScrollIndicator = NO;
    collectionView.showsHorizontalScrollIndicator = NO;
    
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.backgroundColor = [UIColor whiteColor];

    [self.view addSubview:collectionView];
    self.scrollView = collectionView;
    self.scrollView.pagingEnabled = YES;
    //注册cell(如果缓存池中没有HMCellIdentifier对应的cell，就会自动创建HMCellIdentifier对应的注册过的cell)
    [self.scrollView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"tableView"];
    
}
#pragma mark - UICollectionViewDataSource
/**
 *  循环次数，也就是cell的个数
 */
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 3;
}

/**
 *  组数，也就是有多少组的cell
 */
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //创建图片显示单元
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"tableView" forIndexPath:indexPath];
    cell.contentMode = UIViewContentModeBottom;
//    cell.backgroundColor = [UIColor blueColor];
//    cell.alpha = 0.1;
    return cell;
}


#pragma mark - 按钮监听方法
/**
 *  发表
 */
- (void)leftCompile
{
    WKComposeViewController *composeVc = [[WKComposeViewController alloc] init];
    
    //以模态的形式弹出发帖控制器
    //    [self.navigationController presentModalViewController:composeVc animated:YES];//次方法已经过时
    
    [self.navigationController presentViewController:composeVc animated:NO completion:^{
        
    }];
}

/**
 *  刷新
 */
- (void)rightRefresh:(UIBarButtonItem *)btn
{
    
    
}

/**导航栏按钮点击监听*/
- (void)cilck:(UIButton *)btn
{
    switch (btn.tag) {
        case ButtonTypeOne:
            [self.scrollView scrollRectToVisible:CGRectMake(0, 0, WKScreenW, WKChildViewH) animated:NO];
            break;
        case ButtonTypeTwo:
            
            [self.scrollView scrollRectToVisible:CGRectMake(WKScreenW, 0, WKScreenW, WKChildViewH) animated:NO];
            break;
        case ButtonTypeThree:
            [self.scrollView scrollRectToVisible:CGRectMake(2*WKScreenW, 0, WKScreenW, WKChildViewH) animated:NO];
            break;
        default:
            break;
    }
}

#pragma mark - UICollectionViewDelegate
/**
 *  collectionView滚动时调用
 */
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
     //self.scrollView.frame = CGRectMake(0, -64, 320, 580);
    //计算当前的页码
    self.currentPage = (int)(scrollView.contentOffset.x / scrollView.bounds.size.width+0.5) % self.scrollView.subviews.count;
    switch (self.currentPage) {
        case 0:
            self.selectedBtnOne.selected = YES;
            self.selectedBtnTwo.selected = NO;
            self.selectedBtnThree.selected = NO;
            break;
        case 1:
            
            self.selectedBtnTwo.selected = YES;
            self.selectedBtnOne.selected = NO;
            self.selectedBtnThree.selected = NO;
            break;
        case 2:
            self.selectedBtnThree.selected = YES;
            self.selectedBtnOne.selected = NO;
            self.selectedBtnTwo.selected = NO;
            break;
        default:
            break;
    }
}

@end
